package com.loubii.sunshineSchool.ui.avtivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loubii.sunshineSchool.FinalVirable;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.ui.dialog.ListPopWindow;
import com.loubii.sunshineSchool.util.DensityUtil;
import com.loubii.sunshineSchool.R;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;


public class SSAddActivity extends BaseToolBarActivity {

    private int type = -1;
    @BindView(R.id.tv_select_type)
    TextView textType;
    @BindView(R.id.et_remind_content_title)
    EditText title;
    @BindView(R.id.et_remind_content)
    EditText text;
    ListPopWindow list2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_remind_add;
    }

    @Override
    protected void initView() {
        setTitle("发布活动");
    }

    @OnClick({R.id.btn_post, R.id.tv_select_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_post:
                if (type == -1) {
                    Toast.makeText(this, "请选择活动类型~", Toast.LENGTH_LONG).show();
                    return;
                } else if (TextUtils.isEmpty(title.getText())) {
                    Toast.makeText(this, "请输入标题~", Toast.LENGTH_LONG).show();
                } else if (TextUtils.isEmpty(text.getText())) {
                    Toast.makeText(this, "请输入活动内容~", Toast.LENGTH_LONG).show();
                }
                HttpBin.setUpActivity(this, type, title.getText().toString(), text.getText().toString(), new HttpBin.ResponseListener<Boolean>() {
                    @Override
                    public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                        if (response.body()) {
                            Toast.makeText(SSAddActivity.this, "添加成功~", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Boolean> call, Throwable t) {

                    }
                });
                break;
            case R.id.tv_select_type:
                list2 = new ListPopWindow(this, 4);
                list2.setAnchorView(view.findViewById(R.id.tv_select_type));
                list2.setHorizontalOffset(DensityUtil.dip2px(5));
                list2.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return FinalVirable.types.size();
                    }

                    @Override
                    public Object getItem(int i) {
                        return null;
                    }

                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;

                        if (convertView == null) {
                            convertView = LayoutInflater.from(SSAddActivity.this).inflate(R.layout.item_chart_popup, null);
                            holder = new ViewHolder();
                            holder.mTvClassify = (TextView) convertView.findViewById(R.id.tv_classify);
                            convertView.setTag(holder);
                        } else {
                            holder = (ViewHolder) convertView.getTag();
                        }
                        holder.mTvClassify.setText(FinalVirable.types.get(position));
                        return convertView;
                    }

                    class ViewHolder {
                        TextView mTvClassify;
                    }
                });
                list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        textType.setText(FinalVirable.types.get(position));
                        type = position;
                        list2.dismiss();
                    }
                });
                list2.show();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
